#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""GIT assertion

The assertion returned by the IDP.
"""

__author__ = 'Xin Zhou(zhounew@google.com)'

from git.json import git_json

class GitAssertion:
  def __init__(self, identifier, verified_email):
    if verified_email:
      self.verified_email_ = verified_email.lower()
    else:
      self.verified_email_ = ''
    self.first_name_ = ''
    self.last_name_ = ''
    self.photo_url_ = ''
    self.nick_name_ = ''
    self.full_name_ = ''
    self.identifier_ = identifier
  
  def GetVerifiedEmail(self):
    return self.verified_email_
  
  def GetFirstName(self):
    return self.first_name_
  
  def GetLastName(self):
    return self.last_name_
  
  def GetPhotoUrl(self):
    return self.photo_url_
  
  def GetNickName(self):
    return self.nick_name_
  
  def GetFullName(self):
    return self.full_name_
  
  def GetIdentitier(self):
    return self.identifier_
  
  def SetFirstName(self, first_name):
    self.first_name_ = first_name
  
  def SetLastName(self, last_name):
    self.last_name_ = last_name
  
  def SetPhotoUrl(self, photo_url):
    self.photo_url_ = photo_url
  
  def SetNickName(self, nick_name):
    self.nick_name_ = nick_name
  
  def SetFullName(self, full_name):
    self.full_name_ = full_name
  
  def SetIdentifier(self, identifier):
    self.identifier_ = identifier
  
  def ToJsonString(self):
    obj = {'verifiedEmail':self.verified_email_,
           'firstName':self.first_name_,
           'lastName':self.last_name_,
           'fullName':self.full_name_,
           'nickName':self.nick_name_,
           'photoUrl':self.photo_url_,
           'identifier':self.identifier_}
    return git_json.GitJson().dumps(obj)
  
  @staticmethod
  def FromString(jsonString):
    try:
      if not jsonString:
        return None
      obj = git_json.GitJson().loads(jsonString)
      if obj and 'identifier' in obj and 'verifiedEmail' in obj:
        ret = GitAssertion(obj['identifier'], obj['verifiedEmail'])
        if 'firstName' in obj:
          ret.SetFirstName(obj['firstName'])
        if 'lastName' in obj:
          ret.SetLastName(obj['lastName'])
        if 'photoUrl' in obj:
          ret.SetPhotoUrl(obj['photoUrl'])
        if 'nickName' in obj:
          ret.SetNickName(obj['nickName'])
        if 'fullName' in obj:
          ret.SetFullName(obj['fullName'])
        return ret
      else:
        return None
    except Exception, e:
      return None